#!/usr/bin/env python
# encoding: utf-8
# @author: qiaowei
# @license: (C) Copyright 2013-2019
# @contact: weiweiqiao@126.com
# @file: data_processing.py
# @time: 2020/7/28 0028
# @desc:


from PySide2.QtCore import QObject
from PySide2.QtCore import QFileInfo

from py_package.information_module import Information
from py_package.table_model_module import TableModel


class DataProecesing(QObject):
    """
    根据打开的文件，处理文件数据
    """
    def __init__(self, parent=None):
        super().__init__(parent)
        self.__table_model = None

    def read_data_from_txt_file(self, file_name_and_path):
        """
        根据文件名及路径file_name_and_path打开文件并读取数据，将打开的文件名和保存数据的模型model，
        以元组格式返回

        Args:
            file_name_and_path: 文件名及路径的字符串

        Returns:
            元组，保存文件名和保存数据的model

        """
        information_list = []

        # 以只读方式打开文件，允许缓冲，编码格式为utf-8
        with open(file_name_and_path, 'r', buffering=True, encoding='utf-8') as file:
            # 读取文件信息并保存
            for line in file.readlines():
                line = line.replace('\n', '')
                line = line.split('-')
                temp = Information(line[0], float(line[1]))
                information_list.append(temp)

        # 提取文件名（不包括后缀）
        text = QFileInfo(file_name_and_path).fileName(
        ).lower().replace('.txt', '')

        if 0 != len(information_list):
            return text, self.set_model(information_list)
        else:
            return text, None

    def set_model(self, informations: list):
        """
        将列表list保存到模型model

        Args:
            informations: 保存文件的列表list

        Returns:
            保存数据的模型model

        """
        if self.__table_model is None:
            self.__table_model = TableModel()

        self.__table_model.set_model(informations)

        return self.__table_model

    def calculate_frequency(self, value: float):
        information_list = self.__table_model.informations
        count = len(information_list)
        for index in range(0, count):
            if information_list[index].inter_modulation:
                information_list[index].inter_modulation = False
                information_list[index].inter_modulation_frequency = 0

        information_list = self.__test_frequency(
            count, value, information_list)

        # 更新模型
        self.__table_model.beginResetModel()
        self.__table_model.set_model(information_list)
        self.__table_model.endResetModel()

    def __test_frequency(
            self,
            count: int,
            value: float,
            information_list: list):
        """
        根据公式计算传入频率与台站频率是否有互调干扰，更新保存台站频率的list的是否互调、互调频率信息

        Args:
            count: 台站的频率个数
            value: 传入的频率
            information_list: 保存台站频率信息的列表list

        Returns:
            返回更新后的台站频率list

        """
        for index in range(0, count):
            cal = value * 2 - information_list[index].frequency
            for i in range(0, count):
                if 0.0001 > abs(cal - information_list[i].frequency):
                    information_list[index].inter_modulation = True
                    information_list[index].inter_modulation_frequency = cal

        for index in range(0, count):
            cal = information_list[index].frequency * 2 - value
            for i in range(0, count):
                if 0.0001 > abs(information_list[i].frequency - cal):
                    information_list[index].inter_modulation = True
                    information_list[index].inter_modulation_frequency = cal

        return information_list
